﻿// TestCaseGeneratorUI
// (c)2007 Mark Bulmahn. mBulmahn@web.de
// License: GPL 2.0
// Project: TestCaseGenerator. http://www.codeplex.com/TestCaseGenerator

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using TestCaseModel;

namespace TestCaseGenerator.Controls
{
    public partial class ResultRuleExpressionUI : UserControl
    {
        private const string NEW_RULE = "New rule";

        ListViewItem currentItem = null;
        ResultRule currentRule = null;

        TestModel model = null;
        public TestModel Model
        {
            get { return model; }
            set 
            { 
                model = value;
                if (model != null)
                    Resync();
            }
        }


        public ResultRuleExpressionUI()
        {
            InitializeComponent();
        }

        private void Resync()
        {
            currentRule = null;
            currentItem = null;
            listRules.Items.Clear();
            foreach (ResultRule rule in model.ResultRules)
            {
                AddNewRuleToList(rule);
            }
            //listRules.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
        }

        private void AddNewRuleToList(ResultRule rule)
        {
            ListViewItem newLvItem = new ListViewItem(rule.RuleName);
            newLvItem.ImageIndex = 0;
            newLvItem.SubItems.Add(rule.RuleExpression);
            newLvItem.SubItems.Add(rule.ClearTextResult);
            newLvItem.SubItems.Add(rule.ObsoleteTestCase.ToString());
            listRules.Items.Add(newLvItem);
        }


        private void AddNewRule()
        {
            ResultRule newRule = new ResultRule();
            newRule.RuleName = NEW_RULE;
            if (!model.ResultRules.HasResultRule(newRule.RuleName))
            {
                model.ResultRules.Add(newRule);
                AddNewRuleToList(newRule);
            }
            else
            {
                MessageBox.Show("A rulw with the name '" + newRule.RuleName + "' already exists!");
            }
        }

        private void DeleteRule()
        {
            if (currentRule != null)
            {
                model.ResultRules.Remove(currentRule);
                listRules.Items.Remove(currentItem);
            }
        }

        private void EditRule()
        {
            if (currentRule != null)
            {
                FrmRuleExpressionEditor editor = new FrmRuleExpressionEditor();
                editor.Rule = currentRule;
                editor.Model = model;
                editor.Resync();
                editor.ShowDialog();
                currentItem.SubItems.Clear();
                currentItem.Text = currentRule.RuleName;
                currentItem.SubItems.Add(currentRule.RuleExpression);
                currentItem.SubItems.Add(currentRule.ClearTextResult);
            }
        }

        private void butNewCondition_Click(object sender, EventArgs e)
        {
            AddNewRule();
        }

        private void butEdit_Click(object sender, EventArgs e)
        {
            EditRule();
        }

        private void butDelete_Click(object sender, EventArgs e)
        {
            DeleteRule();
        }

        private void listRules_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listRules.SelectedItems.Count > 0)
            {
                currentItem = listRules.SelectedItems[0];
                currentRule = model.ResultRules.GetRuleByName(currentItem.Text);
            }
        }

        private void listRules_DoubleClick(object sender, EventArgs e)
        {
            EditRule();
        }



    }
}
